package com.example.openguass.service.Impl;

import cn.hutool.db.sql.Order;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.openguass.entity.Category;
import com.example.openguass.entity.Dish;
import com.example.openguass.entity.OrderDetail;
import com.example.openguass.entity.Orders;
import com.example.openguass.mapper.OrdersMapper;
import com.example.openguass.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.openguass.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xa
 * @since 2022-11-17
 */
@Service
@Transactional
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {
    private OrdersMapper ordersMapper;
    private OrderDetailService orderDetailService;
    private CategoryService categoryService;
    private DishService dishService;
    private DishFlavorService dishFlavorService;
    @Autowired
    private void setOrdersMapper(OrdersMapper ordersMapper){
        this.ordersMapper = ordersMapper;
    }
    @Autowired
    private void setOrderDetailService(OrderDetailService orderDetailService){
        this.orderDetailService = orderDetailService;
    }
    @Autowired
    private void setCategoryService(CategoryService categoryService){
        this.categoryService = categoryService;
    }
    @Autowired
    private void setDishService(DishService dishService){
        this.dishService = dishService;
    }
    @Autowired
    private void setDishFlavorService(DishFlavorService dishFlavorService){
        this.dishFlavorService = dishFlavorService;
    }

    @Override
    public Boolean insert(List<MenuDishVO> list) {
        double allPrice = 0.0;
        Orders orders = new Orders();
        orders.setCreateDate(LocalDateTime.now());
        orders.setId(UUID.randomUUID().toString());
        for (MenuDishVO menuDishVO : list){
            OrderDetailVO orderDetail = new OrderDetailVO();
            orderDetail.setOrderid(orders.getId());
            BeanUtils.copyProperties(menuDishVO,orderDetail);
            orderDetail.setPrice(orderDetail.getAmount()*orderDetail.getNumber());
            //插入订单详情
            orderDetailService.insert(orderDetail);
            allPrice += orderDetail.getAmount()*orderDetail.getNumber();
        }
        orders.setAmount(allPrice);
        return ordersMapper.insert(orders) > 0;
    }

    @Override
    public Boolean update(OrdersVO ordersVO) {
        if (ordersVO.getStatus().equals("已下单")){
            ordersVO.setStatus("0");
        }else if (ordersVO.getStatus().equals("正在准备")){
            ordersVO.setStatus("1");
        }else if (ordersVO.getStatus().equals("已完成")){
            ordersVO.setStatus("2");
        }
        Orders orders = new Orders();
        BeanUtils.copyProperties(ordersVO,orders);
        orders.setUpdateDate(LocalDateTime.now());
        return ordersMapper.updateById(orders) > 0;
    }

    @Override
    public IPage<OrdersReturnVO> findPage(OrdersPageVO pageVO) {
        Page<Orders> page = new Page<>(pageVO.getPageNo(),pageVO.getPageSize());
        QueryWrapper<Orders> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("create_date");
        Page<Orders> page1 = ordersMapper.selectPage(page, wrapper);
        List<Orders> records = page1.getRecords();
        List<OrdersReturnVO> returnVOList = new ArrayList<>();
        for (Orders order : records){
            OrdersReturnVO ordersReturnVO = new OrdersReturnVO();
            BeanUtils.copyProperties(order,ordersReturnVO);

            //将name赋值到id中
            List<OrderDetail> byOrdersId = orderDetailService.getByOrdersId(order.getId());
            for (OrderDetail orderDetail : byOrdersId){
                Dish dish = dishService.getById(orderDetail.getDishid());
                if (dish != null){
                    orderDetail.setId(dish.getName());
                }
            }

            ordersReturnVO.setOrderDetails(byOrdersId);
            returnVOList.add(ordersReturnVO);
        }
        Page<OrdersReturnVO> voPage = new Page<>();
        BeanUtils.copyProperties(page,voPage);
        voPage.setRecords(returnVOList);
        return voPage;
    }

    @Override
    public List<MenuVO> getMenu() {
        List<MenuVO> menuList = new ArrayList<>();
        List<Category> list = categoryService.list();
        for (Category category : list){
            MenuVO menu = new MenuVO();
            menu.setId(category.getId());
            menu.setName(category.getName());
            //获取当前分类下得所有菜品
            menu.setChild(dishService.getByCategory(category.getName()));
            menuList.add(menu);
        }
        return menuList;
    }

    public AnalysisVO getAnalysisVO(LocalDateTime dateTime){
        QueryWrapper<Orders> wrapper = new QueryWrapper<>();
        wrapper.ge("create_date",dateTime);
        List<Orders> orders = ordersMapper.selectList(wrapper);
        AnalysisVO analysisVO = new AnalysisVO();
        analysisVO.setNum(0);
        analysisVO.setPrice(0.0);
        for (Orders order : orders){
            analysisVO.setNum(analysisVO.getNum()+1);
            analysisVO.setPrice(analysisVO.getPrice()+order.getAmount());
        }
        return analysisVO;
    }

    public AnalysisVO getAnalysisVO(LocalDateTime startDateTime,LocalDateTime endDateTime){
        QueryWrapper<Orders> wrapper = new QueryWrapper<>();
        wrapper.ge("create_date",startDateTime);
        wrapper.le("create_date",endDateTime);
        List<Orders> orders = ordersMapper.selectList(wrapper);
        AnalysisVO analysisVO = new AnalysisVO();
        analysisVO.setNum(0);
        analysisVO.setPrice(0.0);
        for (Orders order : orders){
            analysisVO.setNum(analysisVO.getNum()+1);
            analysisVO.setPrice(analysisVO.getPrice()+order.getAmount());
        }
        return analysisVO;
    }
}
